package com.it.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.it.DictService;
import com.it.dao.CityMapper;
import com.it.dao.DictMapper;
import com.it.dao.ProvinceMapper;
import com.it.pojo.City;
import com.it.pojo.Dict;
import com.it.pojo.Province;
import com.it.pojo.vo.dict.AdminDictVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DictServiceImpl implements DictService {
    @Resource
    private ProvinceMapper provinceMapper;

    @Resource
    private CityMapper cityMapper;

    @Resource
    private DictMapper dictMapper;

    @Override
    public List<Province> listProvine() {
        List<Province> list = provinceMapper.selectList(null);
        return list.stream().map(province -> {
            if (province.getName().contains("内蒙古")) {
                province.setName("内蒙古");
            } else if (province.getName().equals("西藏自治区")) {
                province.setName("西藏");
            } else if (province.getName().contains("广西")) {
                province.setName("广西");
            } else if (province.getName().contains("宁夏")) {
                province.setName("宁夏");
            } else if (province.getName().contains("新疆")) {
                province.setName("新疆");
            } else if (province.getName().contains("香港")) {
                province.setName("香港");
            } else if (province.getName().contains("澳门")) {
                province.setName("澳门");
            }


            return province;
        }).collect(Collectors.toList());
    }

    @Override
    public List<City> listCity() {
        return cityMapper.selectList(null);
    }

    @Override
    public List<City> listCity(String provinceId) {
        LambdaQueryWrapper<City> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(City::getProvinceCode, provinceId);
        return cityMapper.selectList(wrapper);
    }

    @Override
    public List<City> listHotCity() {
        return cityMapper.listHotCity();

    }

    @Override
    public List<AdminDictVO> listDicts() {
        List<Dict> dictList = dictMapper.selectList(null);
        List<AdminDictVO> list = dictList.stream().map(dict -> {
            AdminDictVO adminDictVO = new AdminDictVO();
            BeanUtils.copyProperties(dict, adminDictVO);
            return adminDictVO;
        }).collect(Collectors.toList());
        Map<Integer, List<AdminDictVO>> map = list.stream().collect(Collectors.groupingBy(AdminDictVO::getParentId));
        list.forEach(e -> {
            e.setChildren(map.get(e.getDictId()));
            System.out.println("e = " + e);
        });
        list = list.stream().filter(adminDictVO -> adminDictVO.getDictCode() != null && adminDictVO.getDictId() != 1).collect(Collectors.toList());
        return list;
    }

    @Override
    public Map<String, Object> listParams() {
        Map<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<Dict> wrapper1 = Wrappers.lambdaQuery();
        LambdaQueryWrapper<Dict> wrapper2 = Wrappers.lambdaQuery();
        LambdaQueryWrapper<Dict> wrapper3 = Wrappers.lambdaQuery();
        LambdaQueryWrapper<Dict> wrapper4 = Wrappers.lambdaQuery();
        LambdaQueryWrapper<Dict> wrapper5 = Wrappers.lambdaQuery();
        LambdaQueryWrapper<Dict> wrapper6 = Wrappers.lambdaQuery();
        LambdaQueryWrapper<Dict> wrapper7 = Wrappers.lambdaQuery();
        LambdaQueryWrapper<Dict> wrapper8 = Wrappers.lambdaQuery();

        wrapper1.eq(Dict::getDictCode, "Venue");
        wrapper3.eq(Dict::getDictCode, "traffic");
        wrapper5.eq(Dict::getDictCode, "platform");
        wrapper7.eq(Dict::getDictCode, "delay");
        Dict dict1 = dictMapper.selectOne(wrapper1);
        Dict dict2 = dictMapper.selectOne(wrapper3);
        Dict dict3 = dictMapper.selectOne(wrapper5);
        Dict dict4 = dictMapper.selectOne(wrapper7);
        wrapper4.eq(Dict::getParentId, dict2.getDictId());
        wrapper2.eq(Dict::getParentId, dict1.getDictId());
        wrapper6.eq(Dict::getParentId, dict3.getDictId());
        wrapper8.eq(Dict::getParentId, dict4.getDictId());
        List<Dict> dictList1 = dictMapper.selectList(wrapper2);  // 场馆设施的数据
        List<Dict> dictList2 = dictMapper.selectList(wrapper4);  // 交通方式的数据
        List<Dict> dictList3 = dictMapper.selectList(wrapper6); // 平台热度
        List<Dict> dictList4 = dictMapper.selectList(wrapper8);
        map.put("venueList", dictList1);
        map.put("trafficList", dictList2);
        map.put("platformList", dictList3);
        map.put("delayList", dictList4);
        return map;
    }
}
